package alo.p7.work;

import alo.p7.Heap;

/**
 * @ClassName DeleteAny
 * @Description 删除堆内的任意元素
 * @Author Fanxc
 * @DATE 2021/3/10 14:57
 * @Version 1.0
 */
public class DeleteAny {

    void deleteAny(Heap heap, int data){
        int key =-1;
        //查找元素
        for (int i = 0; i < heap.count; i++) {
            if (heap.array[1]==data){
                key=i;
                break;
            }
        }
        //删除元素
        if (key==-1){
            System.out.println("堆中无此元素");
        }else {
            delete(heap, key);
        }

    }

    private void delete(Heap heap, int i) {
        int key;
        key= heap.array[i];
        heap.array[i]= heap.array[heap.count-1];
        heap.array[heap.count-1]=key;
        heap.count--;
        heap.PercolateDown(i);
    }
}
